public class LeetCode1004 {
    public static void main(String[] args) {
        int[]  nums = new int[]{1,1,1,0,0,0,1,1,1,1,0};
        int k = 2;
        LeetCode1004 leetCode1004 = new LeetCode1004();
        System.out.println(leetCode1004.longestOnes(nums, k));
    }

    public int longestOnes1(int[] nums, int k) {
        int max = 0;
        int current = 0;

        int lIndex;
        int rIndex;
        int zeroIndex;
        int zeroNum = 0;
        for(int i = 0; i < nums.length; i++){
            if(nums[i] == 0){
                zeroNum++;


            }
        }

        return max;
    }

    // 不够优雅
    public int longestOnes(int[] nums, int k) {
        int max = 0;
        int current = 0;

        int kNum = 0;
        int lIndex = 0;
        for(int i = 0; i < nums.length; i++){
            if(nums[i] == 0){
                if(kNum < k){
                    kNum++;
                    if(kNum == 1){
                        lIndex = i + 1;
                    }
                }else{
                    kNum = 0;

                    if(current > max){
                        max = current;
                    }
                    current = 1;


                    if(k == 0){
                        i = i+1;
                    }else {
                        i = lIndex;
                    }
                    System.out.println("11 i:" + i + "  lIndex:" + lIndex + "  kNum:" + kNum + "  current:" + current + "  max:" + max);
                    continue;
                }

            }
            current++;
            System.out.println("i:" + i + "  lIndex:" + lIndex + "  kNum:" + kNum + "  current:" + current + "  max:" + max);

        }
        if(current > max){
            max = current--;
        }
        return max;
    }
}
